const strFormat = require("../../util/strFormat");
const sql = account => {
  return strFormat(
    "select * from admin " + 
    "where account='{account}';",
    { account: account }
  );
};

const login = (connection, req, res) => {
  connection.query(sql(req.body.data.account), (err, result) => {
    var data = null;

    if ( err ) {
      console.error(err);
      res.status(200).json({
        status: "fail",
        data: data,
      });
      return;
    } else {
      if ( result.length === 0 ) {
        data = {
          condition: "warning",
        };
      } else {
        if ( req.body.data.password !== result[0].password ) {
          data = {
            condition: "fail",
          };
        } else {
          data = {
            condition: "success",
            id: result[0].id,
          };
        }
      }

      res.status(200).json({
        status: "success",
        data: data,
      });
    }
  });
}

module.exports = login;